public class PercolationStats {

	private double[] stats;

	public PercolationStats(int N, int trials) {

		if (N <= 0 || trials <= 0) {
			throw new IllegalArgumentException(
					"The number of trials or the grid size is 0.");
		}
		stats = new double[trials];
		for (int attempt = 0; attempt < trials; attempt++) {
			Percolation perc = new Percolation(N);
			int steps = 0;
			while (!perc.percolates()) {
				int iR = StdRandom.uniform(1, N + 1);
				int jR = StdRandom.uniform(1, N + 1);
				if (!perc.isOpen(iR, jR)) {
					perc.open(iR, jR);
					steps++;
				}
			}
			stats[attempt] = (double) steps / (N * N);
		}
	}

	public double mean() {
		return StdStats.mean(stats);

	}

	public double stddev() {
		return StdStats.stddev(stats);

	}

	public double confidenceLo() {
		return mean() - (1.96 * stddev()) / Math.sqrt(stats.length);
	}

	public double confidenceHi() {
		return mean() + (1.96 * stddev()) / Math.sqrt(stats.length);

	}

	public static void main(String[] args) {
		int N = Integer.parseInt(args[0]);
		int trials = Integer.parseInt(args[1]);
		PercolationStats stats = new PercolationStats(N, trials);
		StdOut.print("Mean is : " + stats.mean());
		StdOut.print("StdDev is : " + stats.stddev());
	}
}
